/**
 * AI助手实体类
 * 表示AI侦探助手的配置和功能
 */
export enum AIAssistantPersonality {
  PROFESSIONAL = 'professional',    // 专业型
  FRIENDLY = 'friendly',          // 友好型
  SHERLOCK = 'sherlock',          // 福尔摩斯风格
  WATSON = 'watson',              // 华生风格
  TECHNICAL = 'technical'          // 技术型
}

export enum AIAssistantSkill {
  EVIDENCE_ANALYSIS = 'evidence_analysis',      // 证据分析
  SUSPECT_PROFILING = 'suspect_profiling',      // 嫌疑人画像
  CASE_STRATEGY = 'case_strategy',             // 案件策略
  CLUE_CONNECTION = 'clue_connection',         // 线索关联
  TIMELINE_ANALYSIS = 'timeline_analysis',       // 时间线分析
  MOTIVE_DETECTION = 'motive_detection',        // 动机识别
  PATTERN_RECOGNITION = 'pattern_recognition',   // 模式识别
  FORENSIC_ASSISTANCE = 'forensic_assistance'    // 法医协助
}

export interface AIAssistantConfig {
  personality: AIAssistantPersonality;
  name: string;
  voiceEnabled: boolean;
  voiceSpeed: number; // 0.5-2.0
  language: string; // 'zh-CN', 'en-US', etc.
  skills: AIAssistantSkill[];
  confidenceThreshold: number; // 0-100
  verbosity: 'concise' | 'normal' | 'detailed';
  humorLevel: number; // 0-100
  formality: 'casual' | 'professional' | 'formal';
  customPrompts: Record<string, string>;
}

export interface AIAssistantStats {
  casesHelped: number;
  hintsGiven: number;
  accuracyRate: number; // 0-100
  userRating: number; // 1-5
  totalInteractions: number;
  lastActive: string;
  favoriteSkill: AIAssistantSkill;
  learningProgress: number; // 0-100
}

export interface AIAssistant {
  id: string;
  config: AIAssistantConfig;
  stats: AIAssistantStats;
  isActive: boolean;
  isLearning: boolean;
  version: string;
  createdAt: string;
  updatedAt: string;
}

/**
 * 创建AI助手的工厂函数
 */
export function createAIAssistant(data: Partial<AIAssistant> = {}): AIAssistant {
  const now = new Date().toISOString();
  
  return {
    id: data.id || generateAIAssistantId(),
    config: data.config || createDefaultConfig(),
    stats: data.stats || createDefaultStats(),
    isActive: data.isActive !== undefined ? data.isActive : true,
    isLearning: data.isLearning !== undefined ? data.isLearning : true,
    version: data.version || '1.0.0',
    createdAt: data.createdAt || now,
    updatedAt: data.updatedAt || now,
    ...data
  };
}

/**
 * 创建默认配置
 */
export function createDefaultConfig(): AIAssistantConfig {
  return {
    personality: AIAssistantPersonality.PROFESSIONAL,
    name: 'AI侦探助手',
    voiceEnabled: false,
    voiceSpeed: 1.0,
    language: 'zh-CN',
    skills: [
      AIAssistantSkill.EVIDENCE_ANALYSIS,
      AIAssistantSkill.SUSPECT_PROFILING,
      AIAssistantSkill.CASE_STRATEGY,
      AIAssistantSkill.CLUE_CONNECTION
    ],
    confidenceThreshold: 70,
    verbosity: 'normal',
    humorLevel: 30,
    formality: 'professional',
    customPrompts: {
      greeting: '您好！我是您的AI侦探助手，很高兴协助您破案。',
      evidenceAnalysis: '让我来分析这份证据...',
      suspectProfiling: '基于现有信息，我来为嫌疑人画像...',
      caseStrategy: '针对这个案件，我建议采取以下策略...',
      hintRequest: '我来给您一些提示...'
    }
  };
}

/**
 * 创建默认统计信息
 */
export function createDefaultStats(): AIAssistantStats {
  const now = new Date().toISOString();
  
  return {
    casesHelped: 0,
    hintsGiven: 0,
    accuracyRate: 85,
    userRating: 4.5,
    totalInteractions: 0,
    lastActive: now,
    favoriteSkill: AIAssistantSkill.EVIDENCE_ANALYSIS,
    learningProgress: 0
  };
}

/**
 * 生成AI助手ID
 */
function generateAIAssistantId(): string {
  const timestamp = Date.now();
  const random = Math.random().toString(36).substr(2, 9);
  return `AI-${timestamp}-${random}`;
}

/**
 * 获取AI助手个性的中文名称
 */
export function getAIAssistantPersonalityName(personality: AIAssistantPersonality): string {
  const personalityNames = {
    [AIAssistantPersonality.PROFESSIONAL]: '专业型',
    [AIAssistantPersonality.FRIENDLY]: '友好型',
    [AIAssistantPersonality.SHERLOCK]: '福尔摩斯风格',
    [AIAssistantPersonality.WATSON]: '华生风格',
    [AIAssistantPersonality.TECHNICAL]: '技术型'
  };
  return personalityNames[personality];
}

/**
 * 获取AI助手技能的中文名称
 */
export function getAIAssistantSkillName(skill: AIAssistantSkill): string {
  const skillNames = {
    [AIAssistantSkill.EVIDENCE_ANALYSIS]: '证据分析',
    [AIAssistantSkill.SUSPECT_PROFILING]: '嫌疑人画像',
    [AIAssistantSkill.CASE_STRATEGY]: '案件策略',
    [AIAssistantSkill.CLUE_CONNECTION]: '线索关联',
    [AIAssistantSkill.TIMELINE_ANALYSIS]: '时间线分析',
    [AIAssistantSkill.MOTIVE_DETECTION]: '动机识别',
    [AIAssistantSkill.PATTERN_RECOGNITION]: '模式识别',
    [AIAssistantSkill.FORENSIC_ASSISTANCE]: '法医协助'
  };
  return skillNames[skill];
}

/**
 * 获取AI助手个性颜色
 */
export function getAIAssistantPersonalityColor(personality: AIAssistantPersonality): string {
  const personalityColors = {
    [AIAssistantPersonality.PROFESSIONAL]: '#3498db',
    [AIAssistantPersonality.FRIENDLY]: '#2ecc71',
    [AIAssistantPersonality.SHERLOCK]: '#8b4513',
    [AIAssistantPersonality.WATSON]: '#d35400',
    [AIAssistantPersonality.TECHNICAL]: '#9b59b6'
  };
  return personalityColors[personality];
}

/**
 * 获取AI助手技能颜色
 */
export function getAIAssistantSkillColor(skill: AIAssistantSkill): string {
  const skillColors = {
    [AIAssistantSkill.EVIDENCE_ANALYSIS]: '#3498db',
    [AIAssistantSkill.SUSPECT_PROFILING]: '#e74c3c',
    [AIAssistantSkill.CASE_STRATEGY]: '#f39c12',
    [AIAssistantSkill.CLUE_CONNECTION]: '#2ecc71',
    [AIAssistantSkill.TIMELINE_ANALYSIS]: '#9b59b6',
    [AIAssistantSkill.MOTIVE_DETECTION]: '#e67e22',
    [AIAssistantSkill.PATTERN_RECOGNITION]: '#1abc9c',
    [AIAssistantSkill.FORENSIC_ASSISTANCE]: '#34495e'
  };
  return skillColors[skill];
}

/**
 * 生成AI助手提示消息
 */
export function generateAIHint(
  assistant: AIAssistant,
  type: 'evidence' | 'suspect' | 'case' | 'general',
  context: Record<string, any> = {}
): string {
  const { personality, verbosity, humorLevel } = assistant.config;
  
  let baseMessage = '';
  let detailLevel = '';
  
  switch (type) {
    case 'evidence':
      baseMessage = '基于证据分析，我认为...';
      break;
    case 'suspect':
      baseMessage = '关于嫌疑人，我的看法是...';
      break;
    case 'case':
      baseMessage = '针对这个案件，我建议...';
      break;
    default:
      baseMessage = '让我来协助您...';
  }
  
  switch (verbosity) {
    case 'concise':
      detailLevel = '简要地说：';
      break;
    case 'detailed':
      detailLevel = '详细分析如下：';
      break;
    default:
      detailLevel = '我的分析是：';
  }
  
  // 根据个性调整语气
  if (personality === AIAssistantPersonality.SHERLOCK) {
    baseMessage = '根据我的观察和推理...';
  } else if (personality === AIAssistantPersonality.WATSON) {
    baseMessage = '根据我的记录和分析...';
  }
  
  return `${detailLevel} ${baseMessage}`;
}